var $ = require('../../lib/jquery');
var util = require('./common/util');
var nav = require('./common/nav');
var _header = require('./common/header');
var minHeight = require('./common/minHeight');
var inputlimit = require('./common/input');
inputlimit.init();
minHeight.init();
var _nav = new nav();
var header = new _header();
var zwjc = {
  time: function () {
    var date = new Date();
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    return y + '-' + m + '-' + d ;
  },
  xt: function () { // 血糖
    var _this=this;
    var time;
    $('.js_xt_time').on('click','span', function(){
      $('.js_xt_time').find('span').removeClass('on');
      $(this).addClass('on');
      time=$(this).data().id;
    })

    $('.js_xt_save').on('click', function(){
      var value = $('.js_xt_text').find('input').val();
      if (!time){
        alert('请选择血糖测量的时间')
      }else if (!value){
        alert('请输入血糖检测值')
      }else {
        util.fetch('api/blood/saveBloodGlucose',{
          sign:1,
          name: value,
          paramType:1,
          addDate:_this.time(),
          strType:time,
          egoId:1
        },{
          timeout:10000
        }).then(function(res){
              alert('保存成功');
              $('.js_xt_time').find('span').removeClass('on');
              $('.js_xt_text').find('input').val('');
            
        }).fail(function(res) {
           alert(res.msg);
        })
      }
    })
    $('.js_blood1').on('click',function(){
      window.location.href='./xtjl.html?type=1'
    })
  },
  xy: function () { // 血压
    var _this=this;
    $('.js_xy_save').on('click', function(){
      var hight = $('.js_hight_xy').val();
      var low = $('.js_low_xy').val();
      if (hight && low){
        util.fetch('api/blood/saveBloodGlucose', {
          addDate: _this.time(),
          paramType: 2,
          hightVoltage: hight,
          basePressure: low,
        }, {
          timeout: 10000
        }).then(function(res) {
         
            alert('保存成功');
            $('.js_hight_xy').val('');
            $('.js_low_xy').val('');
         
        })
      }else {
        alert('请输入正确的舒张压及收缩压');
      }
    })
    $('.js_blood').on('click', function () {
      window.location.href = './yajl.html'
    })
  },
  sport: function () { // 运动
    var _this=this;
      util.fetch('api/daily/queryList?&page=1&pageSize=10').then(function(res){
        var n='';
        for(var i=0;i<res.data.list.length;i++){
          n+='<option value="'+res.data.list[i].id+'" >'+res.data.list[i].name+'</option>'
        }
        $('.is_selet').html(n);
      }) 
    $('.js_yd').on('click',function(){
      window.location.href='./thistory.html'
    })
    $(".js_sport").on('click',function(){
      var id=$('.is_selet').val();
      var minNmber=$('.js_minute').val();
     
      if(id=='' || minNmber==''){
        alert('请输入时间')
      }else{
        util.fetch('api/daily/saveDaily',{
          sign:1,
          pId:id,	
          addDate:_this.time(),
          minNmber: minNmber
        }).then(function(res){
          $('.js_minute').val('');
          alert('保存成功')
        }).fail(function(res){
          alert(res.msg)
        })
      }
    
    })
  

  },
  eye_: function () { // 视力
    $('.js_eye_save').on('click', function () {
      var left_shi = $('.js_left_shi').val();
      var left_san =$('.js_left_san').val();
      var right_shi = $('.js_right_shi').val();
      var right_san = $('.js_right_san').val();
      if (left_shi && left_san && right_shi && right_san){
        util.fetch('api/vision/saveOrUpdate?',{
          sign:1,
          leftVision: left_shi,
          leftEyeFlash: left_san,
          rightVision: right_shi,
          rightEyeFlash: right_san
        },{
          timeout:10000
        }).then(function(res){
            $('.js_eye_wrap').find('input').val('');
            alert('保存成功')
          
        }).fail(function(res){
          alert(res.msg);
        })
      }else{
        alert('请填写完成的数据')
      }
    })
    $('.js_eye').on('click',function(){
      window.location.href='./eye.html';
    })
  },
  bindevent: function () {
    this.xt();
    this.xy();
    this.eye_();
    this.sport();
    $('.js_index').on('click',function(){
      location.href='./index.html'
    })
  }
}
zwjc.bindevent();